{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9e409ba4",
   "metadata": {},
   "source": [
    "在OOP程序设计中，我们定义一个class的时候，可以从某个现有的class继承，新的class称为子类，\n",
    "被继承的class称为基类，父类或者超类(base class, super class)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7f86a1a",
   "metadata": {},
   "source": [
    "继承最大的好处是子类获得父类全部功能"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36bce8d4",
   "metadata": {},
   "source": [
    "当子类和父类存在相同的方法是，子类的方法会覆盖父类的方法。在代码运行的时候，总是会调用子类的方法。\n",
    "这样就获得了继承的另一个好处：多态。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e5083ed",
   "metadata": {},
   "source": [
    "什么是多态？我们在定义一个class的时候，实际上就定义了一种数据类型，这与Python自带的数据类型没有区别。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4b577f8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Animal(object):\n",
    "    def run(self):\n",
    "        print('Animal is running ...')\n",
    "class Dog(Animal):\n",
    "    pass\n",
    "a = list()\n",
    "b = Animal()\n",
    "c = Dog()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "500ae676",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(a, list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8d633a33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(b, Animal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "88f34b5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(c, Dog)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ac7cb518",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(c, Animal)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4288605c",
   "metadata": {},
   "source": [
    "可以看出c既是Dog，还是Animal，在继承关系中，一个实例的数据类型是某个子类，那他的数据类型也可以被看做是父类，但是反之则不行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8866814",
   "metadata": {},
   "source": [
    "多态的好处就是，当我们需要传入Dog，Cat，Tortoise时，我们只需要接收Animal类型就可以了，然后按照Animal类型进行操作即可。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c3f6749",
   "metadata": {},
   "source": [
    "对于一个变量，我们只需要知道它时Animal类型，无需确切知道它的子类型，就可以放心的调用run方法，无需关注实现细节。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "219825e8",
   "metadata": {},
   "source": [
    "开闭原则："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d91cbade",
   "metadata": {},
   "source": [
    "对扩展开放：允许新增Animal子类"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f859900",
   "metadata": {},
   "source": [
    "对修改封闭：不需要修改依赖Animal类型的函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "093946a9",
   "metadata": {},
   "source": [
    "继承还可以一级一级的继承下来，任何类都可以追溯到根类object。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32a942e4",
   "metadata": {},
   "source": [
    "### 静态语言 vs 动态语言"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0514d43f",
   "metadata": {},
   "source": [
    "对于静态语言（例如java）来说，如果需要传入Animal类型，则传入的对象必须是Animal类型或者它的子类，否则无法调用方法。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75f4fcb6",
   "metadata": {},
   "source": [
    "对于Python来说，则不一定需要传入Animal类型，我们只需要保证传入的对象有一个对应的方法就可以了。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b159f203",
   "metadata": {},
   "source": [
    "这就是动态语言的“鸭子类型”，它并不要求严格的继承体系，一个对象只要“看起来像鸭子，走起路来像鸭子”，那它就可以被看做是鸭子。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e6e19f8",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "775d286f",
   "metadata": {},
   "source": [
    "继承可以把父类的所有功能都能直接拿过来，这样就不必从零做起，子类只需要新增自己持有的方法，也可以把父类不适合的方法覆盖重写。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9203026d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
